// components/loginButton/loginButton.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    openid: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getUserInfo(e) {
      console.log("用户登录...");
      wx.login({
        //获取code
        success: function (res) {
          wx.setStorageSync('userInfo', e.detail.rawData);
          var code = res.code; //返回code
          console.log(code);
          var appId = 'wx7a9b0cafff1a532e';
          var secret = 'f36f0e13e423d8a8930175bba4bb3e2f';
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
            data: {},
            header: {
              'content-type': 'json'
            },
            success: function (res) {
              console.log(res);
              let userInfo = typeof wx.getStorageSync('userInfo') === 'string' ? JSON.parse(wx.getStorageSync('userInfo')) : wx.getStorageSync('userInfo');
              var openid = res.data.openid //返回openid
              console.log('openid为' + openid);
              request({
                url: 'user/userinfo',
                data: {
                  user_name: userInfo.nickName,
                  user_icon: userInfo.avatarUrl,
                  open_id: openid
                }
              }).then((result) => {
                console.log(result);
                wx.setStorageSync('user', result.data.data);
                if (result.data.data.user_phone === null) {
                  console.log(1111);
                  wx.navigateTo({
                    url: '../../pages/phoneNum/phoneNum'
                  })
                } else {
                  wx.reLaunch({
                    url: '../../pages/index/index',
                  });
                }
              })
            }
          })
        }
      })
    },
  }
})